package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sumpu.app.guoguojie.dao.FamilyDao;
import cn.sumpu.app.guoguojie.domain.Family;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class FamilyDaoImpl extends SqlSessionDaoSupport implements FamilyDao {

	private static Logger logger = LoggerFactory.getLogger(FamilyDaoImpl.class);

	public void insert(Family family) throws AppBusinessException {
		try {
			getSqlSession().insert(
					"cn.sumpu.app.guoguojie.persistence.FamilyMapper.insert",
					family);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Family> getFamilyList(Map<String,Object> map) throws AppBusinessException {
		try {
			return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.FamilyMapper.getFamilyList",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}

	@Override
	public void updateFamily(Family family) throws AppBusinessException {
		try {
			getSqlSession().update("cn.sumpu.app.guoguojie.persistence.FamilyMapper.updateFamily",family);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("update family failure.", e);
			}
			throw new AppBusinessException("update family failure.", e);
		}
	}
	
	//通过会员编号查询得到家的信息
	public Family getFamilyByMember(String member) throws AppBusinessException {
		try {
			return (Family) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.FamilyMapper.getFamilyByMember",member);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getFamilyByMember database failure.", e);
			}
			throw new AppBusinessException("getFamilyByMember database failure.", e);
		}
	}
	

}
